package com.yxb.cms.service;

import com.yxb.cms.architect.constant.BussinessCode;
import com.yxb.cms.architect.utils.BussinessMsgUtil;
import com.yxb.cms.dao.TeachersMapper;
import com.yxb.cms.domain.bo.BussinessMsg;
import com.yxb.cms.domain.bo.ExcelExport;
import com.yxb.cms.domain.vo.ClassTeach;
import com.yxb.cms.domain.vo.SystemLog;
import com.yxb.cms.domain.vo.Teachers;
import com.yxb.cms.enums.YesOrNo;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.nutz.json.Json;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/** 
* @Description: 教师服务
* @Param:  
* @return:  
* @Author: yst
* @Date: 2019/3/28 
*/ 
@Service
public class TeachersService {

    private Logger log = LogManager.getLogger(TeachersService.class);

    @Autowired
    private TeachersMapper teachersMapper;

    /**
     * 插入教师
     * @param teachers 教师实体
     */
    @Transactional
    public BussinessMsg insertTeacherdents(Teachers teachers, String currUserName){

       try {
            Date now = new Date();
           teachers.setTcode(generateSno());
           teachers.setCreateTime(now);
           teachers.setCreator(currUserName);
           teachers.setUpdateTime(now);
           teachers.setModifier(currUserName);
           teachers.setDeleteFlage(YesOrNo.NO.getValue());
           teachersMapper.insertSelective(teachers);
        } catch (Exception e) {
            log.error("保存教师信息方法内部错误",e);
            throw e;
        }
        return BussinessMsgUtil.returnCodeMessage(BussinessCode.GLOBAL_SUCCESS);
    }



    /**
     * 更新教师
     * @param teachers 教师实体
     */
    @Transactional
    public BussinessMsg updateByPrimaryKeySelective(Teachers teachers, String currUserName){
        try {
            teachers.setUpdateTime(new Date());
            teachers.setModifier(currUserName);
            teachersMapper.updateByPrimaryKeySelective(teachers);
        } catch (Exception e) {
            log.error("保存教师信息方法内部错误",e);
            throw e;
        }
        return BussinessMsgUtil.returnCodeMessage(BussinessCode.GLOBAL_SUCCESS);
    }

    /**
     * 查询教师
     * @param tid 教师ID
     */
    public Teachers queryTeacherdentByTeacherId(Long tid){
        //systemLogMapper.updateByPrimaryKeySelective(systemLog);
        Teachers teachers = teachersMapper.selectByPrimaryKey(tid);
        return teachers;
    }

    /**
     * 删除教师
     * @param tid 教师ID
     */
    @Transactional
    public BussinessMsg deleteByPrimaryKeySelective(Long tid){
        try {
            Teachers teachers = new Teachers();
            teachers.setDeleteFlage(YesOrNo.YES.getValue());
            teachers.setId(tid);
            teachersMapper.updateByPrimaryKeySelective(teachers);
        } catch (Exception e) {
            log.error("删除教师信息方法内部错误",e);
            throw e;
        }
        return BussinessMsgUtil.returnCodeMessage(BussinessCode.GLOBAL_SUCCESS);
    }


    /**
     * 教师信息分页显示
     * @param teachers 教师实体
     * @return
     */
    public String selectTeacherdentsPageList(Teachers teachers){

        List<Teachers> TeacherdentList = teachersMapper.selectTeachersPage(teachers);

        Long count = teachersMapper.countByExample(teachers);

        Map<String, Object> map = new HashMap<String, Object>();
        map.put("code",0);
        map.put("msg","");
        map.put("count",count);
        map.put("data", TeacherdentList);

        return Json.toJson(map);
    }

    /**
     * 业务日志EXCEL导出
     * @param systemLog 日志实体
     * @return
     */
    public ExcelExport excelExportSystemLogList(SystemLog systemLog){
        ExcelExport excelExport = new ExcelExport();
        List<SystemLog> systemLogList = null;//systemLogMapper.selectSystemLogList(systemLog);
        excelExport.addColumnInfo("日志标题","logTitle");
        excelExport.addColumnInfo("日志类型","logType_Lable");
        excelExport.addColumnInfo("日志请求URL","logUrl");
        excelExport.addColumnInfo("请求方式","logMethod");
        excelExport.addColumnInfo("请求参数","logParams");
        excelExport.addColumnInfo("请求用户","logUserName");
        excelExport.addColumnInfo("请求IP","logIp");
        excelExport.addColumnInfo("IP归属","logIpAddress");
        excelExport.addColumnInfo("请求时间","logStartTime_Lable");
        excelExport.addColumnInfo("耗时(毫秒)","logElapsedTime");


        excelExport.setDataList(systemLogList);
        return excelExport;
    }


    /**
     * 异常日志EXCEL导出
     * @param systemLog 日志实体
     * @return
     */
    public ExcelExport excelExportSysExceptionLogList(SystemLog systemLog){
        ExcelExport excelExport = new ExcelExport();
        List<SystemLog> systemLogList = null;//systemLogMapper.selectSystemLogList(systemLog);
        excelExport.addColumnInfo("日志标题","logTitle");
        excelExport.addColumnInfo("日志类型","logType_Lable");
        excelExport.addColumnInfo("异常方法","logUrl");
        excelExport.addColumnInfo("请求参数","logParams");
        excelExport.addColumnInfo("异常信息","logException");
        excelExport.addColumnInfo("请求用户","logUserName");
        excelExport.addColumnInfo("请求IP","logIp");
        excelExport.addColumnInfo("IP归属","logIpAddress");
        excelExport.addColumnInfo("请求时间","logStartTime_Lable");
        excelExport.addColumnInfo("耗时(毫秒)","logElapsedTime");


        excelExport.setDataList(systemLogList);
        return excelExport;
    }

    //获取教师编号
    private String generateSno(){
        long time = new Date().getTime();
        String s = String.valueOf(time);
        String sno = s.substring(s.length() - 5, s.length() - 1);
        return sno;
    }


    /**
     * 查询教师下所有班级
     * @param classId 班级id
     */
    /*public List<Teacherdents> queryTeacherdentsByClasses(Long classId){
        //systemLogMapper.updateByPrimaryKeySelective(systemLog);
        List<Teacherdents> Teacherdents = teachersMapper.selectByClassId(classId);
        return Teacherdents;
    }*/


    /**
     * 查询班级教师
     * @param classTeach 班级教师实体
     * @return
     */
    public String selectClassTeachPageList(ClassTeach classTeach){

        List<ClassTeach> TeacherdentList = teachersMapper.selectClassTeachPageList(classTeach);

        Long count = teachersMapper.countByExampleClassTeach(classTeach);

        Map<String, Object> map = new HashMap<String, Object>();
        map.put("code",0);
        map.put("msg","");
        map.put("count",count);
        map.put("data", TeacherdentList);
        return Json.toJson(map);
    }

    /**
     * 插入班级教师
     * @param teachers 班级教师
     */
    @Transactional
    public BussinessMsg insertClassTeach(ClassTeach teachers){

        try {
            teachersMapper.insertSelectiveClassTeach(teachers);
        } catch (Exception e) {
            log.error("保存教师信息方法内部错误",e);
            throw e;
        }
        return BussinessMsgUtil.returnCodeMessage(BussinessCode.GLOBAL_SUCCESS);
    }

    /**
     * 删除班级教师
     * @param ctid 教师ID
     */
    @Transactional
    public BussinessMsg deleteByPrimaryKeySelectiveClassTeach(Long ctid){
        try {

            teachersMapper.deleteByPrimaryKeyClassTeach(ctid);
        } catch (Exception e) {
            log.error("删除教师信息方法内部错误",e);
            throw e;
        }
        return BussinessMsgUtil.returnCodeMessage(BussinessCode.GLOBAL_SUCCESS);
    }

    public List<Teachers> queryAllTeachers(){
        return teachersMapper.selectAllTeachers();
    }

}
